#include <iostream>
#include <map>
#include <vector>
using namespace std;

// 模拟网桥自学习功能
class Bridge {
private:
    map<int, int> macTable; // MAC地址 -> 端口（1或2）
public:
    // 处理帧，返回目的端口（-1表示广播）
    int processFrame(int srcMac, int srcPort, int destMac) {
        // 1. 自学习：记录源MAC与源端口的映射
        if (srcMac != 0xF) { // 排除广播源
            macTable[srcMac] = srcPort;
        }

        // 2. 确定目的端口
        if (destMac == 0xF) { // 广播帧
            return -1;
        } else {
            auto it = macTable.find(destMac);
            if (it != macTable.end()) {
                return it->second; // 已知地址，返回对应端口
            } else {
                return -1; // 未知地址，泛洪（广播）
            }
        }
    }

    // 打印MAC地址表
    void printMacTable() {
        cout << "MAC Address Table:" << endl;
        for (auto& pair : macTable) {
            cout << "MAC 0x" << hex << pair.first << dec 
                 << " -> Port " << pair.second << endl;
        }
    }
};

int main() {
    Bridge bridge;
    vector<tuple<int, int, int>> frames = {
        {0x1, 1, 0x2},   // 源MAC 0x1，端口1，目的0x2
        {0x2, 2, 0x1},   // 源MAC 0x2，端口2，目的0x1
        {0x3, 1, 0xF},   // 源MAC 0x3，端口1，广播
        {0x4, 2, 0x3}    // 源MAC 0x4，端口2，目的0x3
    };

    // 处理每一帧
    for (auto& frame : frames) {
        int srcMac = get<0>(frame);
        int srcPort = get<1>(frame);
        int destMac = get<2>(frame);

        int destPort = bridge.processFrame(srcMac, srcPort, destMac);

        cout << "Frame: src=0x" << hex << srcMac << dec 
             << ", srcPort=" << srcPort 
             << ", dest=0x" << hex << destMac << dec << " -> ";
        if (destPort == -1) {
            cout << "Broadcast (all ports except source)" << endl;
        } else {
            cout << "Dest Port: " << destPort << endl;
        }
    }

    // 输出MAC地址表
    bridge.printMacTable();
    return 0;
}